package com.imddy.tweb.util;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Hashtable;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.EncodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;

public class ZxingUtil {
	
	@SuppressWarnings("deprecation")
	public static void enQRCode(String contents, int width, int height, String imgPath) {

		HashMap<EncodeHintType, String> hints = new HashMap<EncodeHintType, String>();
		hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
		try {
			BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints);
			File imgFile = new File(imgPath);
			MatrixToImageWriter.writeToFile(bitMatrix, "png", imgFile);
			//Path path = new File(imgPath).toPath();
			//Path path = Paths.get(imgPath);
			//MatrixToImageWriter.writeToPath(bitMatrix, "png", path);
		} catch (WriterException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	public static String deQRCode(String imgPath) {
		BufferedImage image = null;
		Result result = null;
		
		try {
			image = ImageIO.read(new File(imgPath));
			if (image == null) {
				System.out.println("the QRcode image may be not exit.");
			}
			LuminanceSource source = new BufferedImageLuminanceSource(image);
			BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
			Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
			hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
			result = new MultiFormatReader().decode(bitmap, hints);
			return result.getText();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (NotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@SuppressWarnings("deprecation")
	public static void enBarCode(String contents, int width, int height, String imgPath) {
		int codeWidth = 3 + // start guard
				7 * 6 + // left bars
				5 + // middle guard
				7 * 6 + // right bars
				3; // end guard
		codeWidth = Math.max(codeWidth, width);
		BitMatrix bitMatrix = null;
		try {
			bitMatrix = new MultiFormatWriter().encode(contents,BarcodeFormat.CODE_128, codeWidth, height, null);
			File imgFile = new File(imgPath);
			MatrixToImageWriter.writeToFile(bitMatrix, "png", imgFile);
			//Path path = new File(imgPath).toPath();
			//Path path = Paths.get(imgPath);
			//MatrixToImageWriter.writeToPath(bitMatrix, "png", path);
		} catch (WriterException e) {
			System.out.println("invoke encode error.");
		} catch (IOException e) {
			System.out.println("invoke encode error.");
		}
	}
	
	
	public static String deBarCode(String imgPath) {
		BufferedImage image = null;
		Result result = null;
		try {
			image = ImageIO.read(new File(imgPath));
			if (image == null) {
				System.out.println("the barcode image may be not exit.");
			}
			LuminanceSource source = new BufferedImageLuminanceSource(image);
			BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
			result = new MultiFormatReader().decode(bitmap, null);
			return result.getText();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}